﻿/*
 * Grid
 * 
 */
package com.cf.core.model.diagram.background {
	import com.cf.core.model.diagram.background.axis.Label;
	import flash.display.DisplayObjectContainer;
	import flash.display.Shape;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.sampler.NewObjectSample;
	
	import com.cf.core.events.LoadEvent;
	import com.cf.core.Utils.tools;
	import com.cf.core.Utils.debug;
	
	import com.cf.core.model.diagram.config.*;
	
	public class Grid extends BaseBackground {
		// API parts
		private var G_lineColor:uint;
		public function get lineColor():uint{return G_lineColor}
		public function set lineColor(_n:uint) { G_lineColor = _n }
		private var G_rangeY:range;
		public function set rangeY(_v:range):void{ G_rangeY=_v; }
		public function get rangeY():range { return G_rangeY; }
		// End : API parts
		public function Grid():void {
			RELEASE();
		}
		override public function setup(_prop:param):void {
			super.setup(_prop);		// use this.PARAM to access _prop.
			var _min:Number, _max:Number, _offset:Boolean;
			var _tmp = tools.arrayObjectFilter(_prop.data, ".value");	// transform target object's child to array.
			_min = tools.min( _tmp );	// find min in array
			_max = tools.max( _tmp );	// find max in array
			// 1) try to put value in range.
			this.rangeY = new range(_min, _max, this.PARAM.cellSize, this.toHeight);
			// debug.msg("Grid input cellSize:" + this.PARAM.cellSize + ", distance:["+this.PARAM.height+" - "+this.padding+" * 2]"+ this.toHeight);
			debug.msg(String(this.rangeY));
		}
		override public function RELEASE(e:*= null):void {
			this.GRAPH.graphics.clear();
			this.lineColor = 0x999999;
			G_rangeY = new range(0, 0, 0, 0);	// init
		}//fn
		override public function drawChart():void {
			super.drawChart();
			debug.msg("Hi there I'm Grid");
		}//fn
		/**
		 * Create Grid by;
		 * @param	param
		 */
		private var G_labelArr:Array;
		override public function draw():void {
			super.draw();
			G_labelArr = new Array();
			tools.emptySprite(this.UI);
			G_rangeY.distance = this.toHeight;
			var _unit:Number; // 圖表中的單位
			var borderSize:int = 1;
			var _rows:Number;
			var r:int = 0;
			var i:int = 0;
			var _label:Label;
			var _maxW:Number=0;
			_rows = this.rangeY.row;
			this.GRAPH.graphics.lineStyle(borderSize, lineColor);
			for(r=0; r<=_rows; r++){
				var toY:Number = r * this.rangeY.cellSize;
				if(r<_rows) this.GRAPH.graphics.drawRect(0, toY, this.toWidth, this.rangeY.cellSize);
				// add Label here.
				_label = new Label();
				with (_label) {
					txt.embedfont("Arial");
					txt.size = 12;
					txt.text = (this.rangeY.max - (r * this.rangeY.step));
					x = -3; // -1 * width;
					y = toY; // + (this.rangeY.cellSize-txt.height) / 2;	// middle the label
					RIGHT = true;
					MIDDLE = true;
					roll = 60;
				}//with
				G_labelArr.push(_label);
				this.UI.addChild( G_labelArr[ G_labelArr.length - 1 ] );
			}//for
		}//fn
		override public function toString():String {
			return "com.cf.core.model.diagram.background.Grid";
		}//fn
		
	}//class
}//package
